<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>购物车</title>
<link rel="stylesheet" type="text/css" href="/style/css/base.css"/>
<link rel="stylesheet" type="text/css" href="/style/css/swiper.min.css"/>
<link rel="stylesheet" type="text/css" href="/style/css/xshop.css?ver=2019041602"/>
<link rel="stylesheet" type="text/css" href="/style/css/sweet-alert.css"/>
<script src="/style/js/jquery-1.12.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/style/js/swiper.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/style/js/xShop.js" type="text/javascript" charset="utf-8"></script>
<script src="/style/js/sweet-alert.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="outwrap">
	<div class="in-mian5 cart clearfix">
		<div class="commodity_box gwc-box">
			<?php
			if($this->data['cart'])
			{
				foreach($this->data['cart'] as $r)
				{
					if($this->data['goods'][$r['goods_id']])
					{
						$goods = $this->data['goods'][$r['goods_id']];

						if($r['shop_price']>0)
						{
							$goods_price = number_format($r['number'] * $r['shop_price'],2,".","");
							$total_price += $goods_price;
						}
						elseif ($goods['ximi_price']>0) {
							$goods_price = number_format($r['number'] * $goods['ximi_price'],2,".","");
							$total_price_ximi += $goods_price;
						}
						

						$pic = '/style/images/img22.jpg';

						if($goods['thumb_docid']>0 && $this->data['docs'][$goods['thumb_docid']]) $pic = $this->data['docs'][$goods['thumb_docid']];
						elseif($goods['pic_docid']>0 && $this->data['docs'][$goods['pic_docid']]) $pic = $this->data['docs'][$goods['pic_docid']];
			?>
			<div class="commodity_list gwc-item" id="cart_item_<?=$r['id'];?>">
				<!--商品-->
				<ul class="commodity_list_term">
					<li class="select clearfix">
						<em data-cartid="<?=$r['id'];?>" class="pitch_on"></em>
						<div class="img"><a href="/index/goodsdetail?goods_id=<?=$r['goods_id'];?>"><img src="<?=$pic;?>" /></a></div>
						<div class="div_center">
							<h4><a href="/index/goodsdetail?goods_id=<?=$r['goods_id'];?>"><?=$goods['goods_name'];?></a></h4>
                            <p style="font-size:10px;"><?= $r['attr_str']?></p>
							<p class="now_value"><i></i><b class="qu_su"><?php
							if($r['shop_price']>0)
							{
								echo '￥'.$r['shop_price'];
							}
							elseif($goods['ximi_price']>0)
							{
								echo ($goods['ximi_price']*1).'硒米';
							}
							?></b></p>
							<?php
							if($goods['price_me']>0 || $goods['coupon_price']>0)
							{
								/*echo '<p class="morei">';
								if($goods['price_me']>0)
								{
									echo '返'.($goods['price_me']*1);
								}
								if($goods['coupon_price']>0)
								{
									echo ' 抵券'.$goods['coupon_price'];
								}
								echo '</p>';*/
							}
							?>
						</div>

						<div class="stock_number">库存<?=$r['stocknumber'].$goods['goods_unit'];?></div>
						<div class="div_right">
							<i onClick="reducew(this)">-</i>
							<span class="zi"><?=$r['number'];?></span>
							<input type="hidden" value="<?=$r['number'];?>" data-cartid="<?=$r['id'];?>" data-price="<?=$r['shop_price'];?>" data-ximi="<?=$goods['ximi_price'];?>" data-stocknumber="<?=$r['stocknumber'];?>" id="stocknumber_<?=$r['id'];?>">
							<i onClick="plusw(this)">+</i>
						</div>
					</li>
				</ul>
				<!--操作栏-->
				<div class="shop_ul_bottom">
					<a href="javascript:_remove(<?=$r['id'];?>);">移除</a>
					<p id="goodsprice_<?=$r['id'];?>"></p>					
				</div>
			</div>
			<?
					}
				}
			}
			?>
		</div>
	</div>

	<!--底部导航-->
	<div class="foot-js">
		<div>
		<dl class="all_check select">
        	<dt><span id="all_pitch_on" class="pitch_on"></span><em>全选</em></dt>
        </dl>
        <dl class="total_amount">
        	<dt><p id="total_price"><b><?=number_format($total_price,2,".","");?></b></p></dt>
        </dl>
        <input type="hidden" name="gcs" id="gcs" />
        <a class="settle_btn" href="javascript:_gotoCheckout();" id="confirm_cart">结算(0)</a>
    	</div>
	</div>	
</div>
<form method="get" action="checkout" id="frm">
<input type="hidden" name="cartid" id="checkout_cartid" value="">
</form>
<script type="text/javascript"> 
$(document).ready(function(e)
{
	// 商品选择
	$('.commodity_list_term .select em').on('click',function(){
		if($(this).hasClass('pitch_on')){
			$(this).removeClass('pitch_on');
		}else{
			$(this).addClass('pitch_on');
		}

		_checkSelectAll();
		_refresh_price();
	});
	// 全选
	$(".all_check dt").on("click",function(){

		if( $("#all_pitch_on").hasClass("pitch_on") )
		{
			$(".commodity_list_term .select em,#all_pitch_on").removeClass("pitch_on");

		}
		else $(".commodity_list_term .select em,#all_pitch_on").addClass("pitch_on");

		_refresh_price();
	});

	_refresh_price();
});

/* 加减  */

function _checkSelectAll()
{
	var slk = true;
	$(".commodity_list_term .select em").each(function(i,v){
		if(!$(this).hasClass("pitch_on")) slk = false;
	});

	if(slk) $("#all_pitch_on").addClass("pitch_on");
	else $("#all_pitch_on").removeClass("pitch_on");
}

function reducew(obj)
{
	//减
	var n = 0;
	var $this = $(obj);
	var ise = $this.siblings("span").text();
	var gc_id = $this.siblings("input").val();
	
	if(parseInt(ise) <= 1){
		n = 1;
		$this.siblings("span").text(n);
		$this.siblings("input").prop("value",n);
	}else{
		n =parseInt(ise)-1;
		$this.siblings("span").text(n);
		$this.siblings("input").prop("value",n);
	}

	_update_number($this.siblings("input").attr("data-cartid"),n);
	_refresh_goods_price($this.siblings("input").attr("data-cartid"));
	_refresh_price();
}

function plusw(obj)
{
	//加
	var $this = $(obj);
	var ise = $this.siblings("span").text();
	var _max_stocknumber = parseInt( $this.siblings("input").attr("data-stocknumber") );
	var n = parseInt(ise)+1;
	if(n>_max_stocknumber) n =_max_stocknumber;
	$this.siblings("span").text(n);
	$this.siblings("input").prop("value",n);

	_update_number($this.siblings("input").attr("data-cartid"),n);
	_refresh_goods_price($this.siblings("input").attr("data-cartid"));
	_refresh_price();
}

function _refresh_goods_price(_cartid)
{
	var _price = _number = _goodsprice = 0;

	_price = parseFloat($("#stocknumber_"+_cartid).attr("data-price"));
	_number = parseInt($("#stocknumber_"+_cartid).prop("value"));
	_goodsprice = _price * _number;

	$("#goodsprice_"+_cartid).html('小计:'+_goodsprice.toFixed(2));
		
}

// 更新商品数量
function _update_number(cart_id,goods_number)
{
	$.post("/cart/number_ajax",{"act":"update","cartid":parseInt(cart_id),"goods_number":parseInt(goods_number),"mr":Math.random()},function(msg){
		// console.log(msg);
       if(msg==901){
           swal("已超过限购数量");
          // window.location.reload();
       }
	});
}


// 统计价格
function _refresh_price()
{
	var _total_price = _total_price_ximi = _total_goods = _price = _price_ximi = _number = _cartid = _goodsprice =0;
	$(".commodity_list_term .select em").each(function(i,v){
		if($(this).hasClass("pitch_on"))
		{
			_cartid = parseInt( $(this).attr("data-cartid") );
			_price = parseFloat($("#stocknumber_"+_cartid).attr("data-price"));
			_price_ximi = parseFloat($("#stocknumber_"+_cartid).attr("data-ximi"));
			_number = parseInt($("#stocknumber_"+_cartid).prop("value"));
			if(_price>0)
			{
				_goodsprice = _price * _number;
				_total_price += _goodsprice;
			}
			else if(_price_ximi>0)
			{
				_goodsprice = _price_ximi * _number;
				_total_price_ximi += _goodsprice;
			}
			
			_total_goods += _number;
			
			
			$("#goodsprice_"+_cartid).html('小计 <span><b>'+_goodsprice.toFixed(2)+'</b></span>');
		}
	});

	var _ximi_html = '';

	if(_total_price_ximi>0)
	{
		_ximi_html = '<i>'+(_total_price_ximi * 1)+'硒米</i>';
	}

	$("#total_price b").html('￥'+ _total_price.toFixed(2) + _ximi_html);
	$("#confirm_cart").html('结算('+_total_goods+')');
	
	}
	
// 跳转到结算
function _gotoCheckout()
{
	var goto = false,_cartids = '';
	$(".commodity_list_term .select em").each(function(i,v){
		if( $(this).hasClass("pitch_on") )
		{
			goto = true;
			if(_cartids!="") _cartids += ",";
			_cartids += $(this).attr("data-cartid");
		}
	});

	if(goto)
	{
		$.post('/cart/ajax',{'act':"purchasing_num","cartids":_cartids},function(data){
			 if(data.code==200){
				 //console.log(_cartids);
					$("#checkout_cartid").prop("value",_cartids);
					$("#frm").submit(); 
				}else{
					swal(data.msg);
					}
			},'json');
		
	}
	else swal("请选择至少一款商品");

}

function _checkEmpty()
{
	var goto = true;
	$(".commodity_list_term .select em").each(function(i,v){
		goto = false;
	});

	if(goto)
	{
		window.location.href = '/cart/cart';
	}
}

// 移除商品
function _remove(cartid)
{
	swal({
      title: "确定移除商品吗?",
      type: "warning",
      timer:30000,
      cancelButtonText: "取消",
      showCancelButton: true,
      closeOnConfirm: true,
      closeOnCancel: true
    },
    function(isConfirm){
      if (isConfirm) {
        $.post("goods_ajax",{"act":"remove","cartid":cartid,"mr":Math.random()},function(json){
          	console.log(json);
            if(json==200){              
              $("#cart_item_"+cartid).remove();
              _refresh_price();
              _checkEmpty();
            }
            else swal("移除商品失败，请重试",false,"error");
          
        });
      }
    });
}
</script>
</body>
</html>
